package com.djhu.freepartner.web.service.impl;

import com.djhu.freepartner.web.config.MailConfig;
import com.djhu.freepartner.web.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executor;

@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {

    @Autowired(required = false)
    private MailSender mailSender;

    @Autowired
    private MailConfig mailConfig;

    @Autowired
    private Executor executor;

    @Override
    public boolean send(String to,String title,String content) {
        SimpleMailMessage message = new SimpleMailMessage();//消息构造器
        message.setFrom(mailConfig.getUsername());//发件人
        message.setTo(to);//收件人
        message.setSubject(title);//主题
        message.setText(content);//正文
        executor.execute(new Runnable() {
            @Override
            public void run() {
                mailSender.send(message);
                log.info("email send success");
            }
        });
        return true;
    }




    public static void main(String[] args) {
//        EmailServiceImpl emailService = new EmailServiceImpl();
//        emailService.send("1");
    }
}
